package com.aisile.admin.service.imol;

import com.aisile.admin.mapper.AdSensitiveMapper;
import com.aisile.admin.service.AdSensitiveService;
import com.aisile.model.admin.dtos.SensitiveDto;
import com.aisile.model.admin.pojos.AdSensitive;
import com.aisile.model.common.dtos.PageResponseResult;
import com.aisile.model.common.dtos.ResponseResult;
import com.aisile.model.common.enums.AdminHttpCodeEnum;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {
    /**
     * 分页
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByPage(SensitiveDto dto) {
        //先判断dto是否为空或错误(只判断错误,  不判断对)
        if (dto==null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.NEED_ADMIND);
        }
        //分页初始化
        dto.checkParam();
        //创建条件
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNoneBlank(dto.getName()),AdSensitive::getSensitives,dto.getName());
        //执行分页
        IPage<AdSensitive> iPage = this.page(new Page<>(dto.getPage(), dto.getSize()), lambdaQueryWrapper);
        //将ipage装到请求返回值里
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), Integer.valueOf(iPage.getTotal()+""));
        responseResult.setData(iPage.getRecords());
        return responseResult;
    }

    /**
     * 添加
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult saveAdSensitive(AdSensitive adSensitive) {
        if (adSensitive==null || adSensitive.getSensitives()==null || StringUtils.isEmpty(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AdminHttpCodeEnum.NEED_ADMIND);
        }
        //先查后插
        int i = this.count(
                Wrappers.<AdSensitive>lambdaQuery().eq(
                        AdSensitive::getSensitives,adSensitive.getSensitives()
                )
        );
        //大于0 存在报错
        if (i>0){
            return ResponseResult.errorResult(AdminHttpCodeEnum.NEED_ADMIND);
        }
        //当前时间
        adSensitive.setCreatedTime(new Date());
        //执行添加
        this.save(adSensitive);
        return ResponseResult.errorResult(AdminHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult updateAdSensitive(AdSensitive adSensitive) {
        if (adSensitive==null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.NEED_ADMIND);
        }
        //当前时间
        adSensitive.setCreatedTime(new Date());
        //执行修改
        this.updateById(adSensitive);
        return ResponseResult.errorResult(AdminHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseResult delAdSensitive(Integer id) {
        if (id==null){
            return ResponseResult.errorResult(AdminHttpCodeEnum.NEED_ADMIND);
        }
        this.removeById(id);
        return ResponseResult.errorResult(AdminHttpCodeEnum.SUCCESS);

    }
}
